package sunlight.validation;

import java.awt.Color;

import javax.swing.JPasswordField;
import javax.swing.JTextField;

import sunlight.util.SConverter;


public class validateTextField {
	public static Color 	invalidStateColor = Color.ORANGE;
	public static Color 	validStateColor   = Color.WHITE;
	
	public static boolean 	state	  = false;
	public static boolean[] allStates = null;
	
	public static Validate Validator = new Validate();
	
	public static void initStates(int fieldsnum){
		state 		= false;
		allStates	= new boolean[fieldsnum];
		
		for(int i=0; i<allStates.length; i++){
			allStates[i] = true;
		}
	}
	
	public static boolean minimunLength(int minimun, JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i<fields.length; i++){
				if(!Validator.is_valid_text_length(minimun, fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}

	public static boolean minimunLength(int minimun, JPasswordField... fields){
		String value = null;
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				value = SConverter.fromCharArrayToString(fields[i].getPassword());
				
				if(!Validator.is_valid_text_length(minimun, value)){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean getGlobalState() {
		if(allStates != null){
			if(allStates.length > 0){
				for (int i=0; i<allStates.length; i++){
					if(allStates[i] == false) {
						return false;
					}
				}
			}
		}
		
		return true;
	}
	
	public static boolean numericFloat(JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_float_number(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		return getGlobalState();
	}
	
	public static boolean numericInt(JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_int_number(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean numericFloatSmallerThenMax(int max, JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_smaller_then_number(max, fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean numericIntPositive(JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_positive_number(fields[i].getText().toString()) && !Validator.is_int_number(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean numericFloatPositive(JTextField... fields){
		
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_positive_number(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean ipAddress(JTextField... fields){
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_valid_ip(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean hostname(JTextField... fields){
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_valid_hostname(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean httpURL(JTextField... fields){
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_valid_url(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean cnp(JTextField... fields){
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_valid_cnp(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
	
	public static boolean email(JTextField... fields){
		if(fields.length > 0){
			for(int i=0; i< fields.length; i++){
				
				if(!Validator.is_valid_email(fields[i].getText().toString())){
					fields[i].setBackground(invalidStateColor);
					allStates[i] = false;
				}
				else{
					fields[i].setBackground(validStateColor);
					allStates[i] = true;
				}
			}
		}
		
		return getGlobalState();
	}
}
